package org.jeecg.modules.wlhy.hywarningmessage.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.wlhy.hywarningmessage.entity.HyWarningMessage;
import org.jeecg.modules.wlhy.hywarningmessage.service.IHyWarningMessageService;
import org.jeecg.modules.wlhy.setting.service.IHySettingService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;

/**
 * @Description: 告警消息
 * @Author: duanlikao
 * @Date:   2020-12-22
 * @Version: V1.0
 */
@Api(tags="告警消息")
@RestController
@RequestMapping("/hywarningmessage/hyWarningMessage")
@Slf4j
public class HyWarningMessageController extends JeecgController<HyWarningMessage, IHyWarningMessageService> {

	@Autowired
	private IHyWarningMessageService hyWarningMessageService;

	@Autowired
	private IHySettingService settingService;

	@ApiOperation(value="获取通知公告", notes="获取通知公告")
	@GetMapping(value = "/getNotice")
	public Result<?> getNotice(HttpServletRequest req) {
		String noticePath = this.settingService.getSettingValueByTag("platform_notice");
		String noticeName = this.settingService.getSettingValueByTag("web_platform_name") + "温馨提示";
		JSONObject json = new JSONObject();
		json.put("noticePath", noticePath);
		json.put("noticeName", noticeName);
		return Result.OK(json);
	}

	/**
	 * 分页列表查询
	 *
	 * @param hyWarningMessage
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="告警消息-分页列表查询", notes="告警消息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyWarningMessage hyWarningMessage,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HyWarningMessage> queryWrapper = QueryGenerator.initQueryWrapper(hyWarningMessage, req.getParameterMap());
		queryWrapper.orderByDesc("create_time");
		Page<HyWarningMessage> page = new Page<HyWarningMessage>(pageNo, pageSize);
		IPage<HyWarningMessage> pageList = hyWarningMessageService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	@ApiOperation(value="运单告警消息-分页列表查询", notes="告警消息-分页列表查询")
	@GetMapping(value = "/shippingNoticeList")
	public Result<?> queryPageShippingNoticeList(HyWarningMessage hyWarningMessage,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		LambdaQueryWrapper<HyWarningMessage> queryWrapper = new LambdaQueryWrapper<>();
		if (StrUtil.isNotEmpty(hyWarningMessage.getMessageContent())) {
			queryWrapper.like(HyWarningMessage::getMessageContent, hyWarningMessage.getMessageContent());
		}
		if (ObjectUtil.isNotEmpty(hyWarningMessage.getMessageType())) {
			queryWrapper.eq(HyWarningMessage::getMessageType, hyWarningMessage.getMessageType());
		} else {
			//MESSAGE_TYPE_RECEIVE(200,"已接单"),
			//MESSAGE_TYPE_LOAD(210,"运单装货"),
			//MESSAGE_TYPE_UNLOAD(220,"运单卸货"),
			//MESSAGE_TYPE_SIGN(230,"运单签收"),
			//MESSAGE_TYPE_CHECK(240,"运单对账"),
			//MESSAGE_TYPE_PAY(250,"运单支付"),
			queryWrapper.in(HyWarningMessage::getMessageType,"210","220","230","240","250");
		}

		queryWrapper.orderByDesc(HyWarningMessage::getCreateTime);
		Page<HyWarningMessage> page = new Page<HyWarningMessage>(pageNo, pageSize);
		IPage<HyWarningMessage> pageList = hyWarningMessageService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	@ApiOperation(value="证件告警消息-分页列表查询", notes="证件告警消息-分页列表查询")
	@GetMapping(value = "/documentNoticeList")
	public Result<?> queryPageDocumentNoticeList(HyWarningMessage hyWarningMessage,
												 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		LambdaQueryWrapper<HyWarningMessage> queryWrapper = new LambdaQueryWrapper<>();
		if (StrUtil.isNotEmpty(hyWarningMessage.getMessageContent())) {
			queryWrapper.like(HyWarningMessage::getMessageContent, hyWarningMessage.getMessageContent());
		}
		if (ObjectUtil.isNotEmpty(hyWarningMessage.getMessageType())) {
			queryWrapper.eq(HyWarningMessage::getMessageType, hyWarningMessage.getMessageType());
		} else {
			//    MESSAGE_TYPE_SFZDQ_ERROR(10,"身份证已到期"),
			//    MESSAGE_TYPE_SFZDQ_WARNING(11,"身份证即将到期"),
			//    MESSAGE_TYPE_CYZGZDQ_ERROR(20,"从业资格证已到期"),//
			//    MESSAGE_TYPE_CYZGZDQ_WARNING(21,"从业资格证即将到期"),//
			//    MESSAGE_TYPE_JSZDQ_ERROR(30,"驾驶证已到期"),//
			//    MESSAGE_TYPE_JSZDQ_WARNING(31,"驾驶证即将到期"),//
			//    MESSAGE_TYPE_XSZDQ(40,"行驶证到期"),//
			//    MESSAGE_TYPE_YSZDQ_ERROR(50,"车辆道路运输证已到期"),//
			//    MESSAGE_TYPE_YSZDQ_WARNING(51,"车辆道路运输证即将到期")
			queryWrapper.in(HyWarningMessage::getMessageType,"10","11","20","21","30","31","40","50","51");
		}

		queryWrapper.orderByDesc(HyWarningMessage::getCreateTime);
		Page<HyWarningMessage> page = new Page<HyWarningMessage>(pageNo, pageSize);
		IPage<HyWarningMessage> pageList = hyWarningMessageService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	@ApiOperation(value="告警消息-分页列表查询", notes="告警消息-分页列表查询")
	@GetMapping(value = "/driverlist")
	public Result<?> queryPageDriverList(HyWarningMessage hyWarningMessage,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HyWarningMessage> queryWrapper = QueryGenerator.initQueryWrapper(hyWarningMessage, req.getParameterMap());
		if (StrUtil.isNotEmpty(hyWarningMessage.getMessageContent())) {
			queryWrapper.like("message_content", hyWarningMessage.getMessageContent());
		}
		if (ObjectUtil.isNotEmpty(hyWarningMessage.getMessageType())) {
			queryWrapper.eq("message_type", hyWarningMessage.getMessageType());
		} else {
			//    MESSAGE_TYPE_SFZDQ_ERROR(10,"身份证已到期"),
			//    MESSAGE_TYPE_SFZDQ_WARNING(11,"身份证即将到期"),
			//    MESSAGE_TYPE_CYZGZDQ_ERROR(20,"从业资格证已到期"),//
			//    MESSAGE_TYPE_CYZGZDQ_WARNING(21,"从业资格证即将到期"),//
			//    MESSAGE_TYPE_JSZDQ_ERROR(30,"驾驶证已到期"),//
			//    MESSAGE_TYPE_JSZDQ_WARNING(31,"驾驶证即将到期"),//
			queryWrapper.in("message_type","10","11","20","21","30","31");
		}

		queryWrapper.orderByDesc("create_time");
		Page<HyWarningMessage> page = new Page<HyWarningMessage>(pageNo, pageSize);
		IPage<HyWarningMessage> pageList = hyWarningMessageService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	@ApiOperation(value="告警消息-分页列表查询", notes="告警消息-分页列表查询")
	@GetMapping(value = "/vehiclelist")
	public Result<?> queryPageVehicleList(HyWarningMessage hyWarningMessage,
										 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
										 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
										 HttpServletRequest req) {
		QueryWrapper<HyWarningMessage> queryWrapper = QueryGenerator.initQueryWrapper(hyWarningMessage, req.getParameterMap());
		if (StrUtil.isNotEmpty(hyWarningMessage.getMessageContent())) {
			queryWrapper.like("message_content", hyWarningMessage.getMessageContent());
		}
		if (ObjectUtil.isNotEmpty(hyWarningMessage.getMessageType())) {
			queryWrapper.eq("message_type", hyWarningMessage.getMessageType());
		} else {
			//    MESSAGE_TYPE_XSZDQ(40,"行驶证到期"),//
			//    MESSAGE_TYPE_YSZDQ_ERROR(50,"车辆道路运输证已到期"),//
			//    MESSAGE_TYPE_YSZDQ_WARNING(51,"车辆道路运输证即将到期")
			queryWrapper.in("message_type","40","50","51");
		}

		queryWrapper.orderByDesc("create_time");
		Page<HyWarningMessage> page = new Page<HyWarningMessage>(pageNo, pageSize);
		IPage<HyWarningMessage> pageList = hyWarningMessageService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	/**
	 *   添加
	 *
	 * @param hyWarningMessage
	 * @return
	 */
	@AutoLog(value = "告警消息-添加")
	@ApiOperation(value="告警消息-添加", notes="告警消息-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody HyWarningMessage hyWarningMessage) {
		hyWarningMessageService.save(hyWarningMessage);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param hyWarningMessage
	 * @return
	 */
	@AutoLog(value = "告警消息-编辑")
	@ApiOperation(value="告警消息-编辑", notes="告警消息-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HyWarningMessage hyWarningMessage) {
		hyWarningMessageService.updateById(hyWarningMessage);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "告警消息-通过id删除")
	@ApiOperation(value="告警消息-通过id删除", notes="告警消息-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		hyWarningMessageService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "告警消息-批量删除")
	@ApiOperation(value="告警消息-批量删除", notes="告警消息-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hyWarningMessageService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="告警消息-通过id查询", notes="告警消息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		HyWarningMessage hyWarningMessage = hyWarningMessageService.getById(id);
		if(hyWarningMessage==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hyWarningMessage);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hyWarningMessage
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HyWarningMessage hyWarningMessage) {
        return super.exportXls(request, hyWarningMessage, HyWarningMessage.class, "告警消息");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HyWarningMessage.class);
    }


	@AutoLog(value = "告警消息-通过id关闭")
	@ApiOperation(value="告警消息-通过id关闭", notes="告警消息-通过id关闭")
	@PutMapping(value = "/closeWarning")
	public Result<?> closeWarning(@RequestBody HyWarningMessage hyWarningMessage) {
		LoginUser loginUser = UserUtil.getLoginUser();
		hyWarningMessage.setMessageStatus(2);
		hyWarningMessage.setUpdateBy(loginUser.getUsername());
		hyWarningMessage.setUpdateTime(new Date());
		hyWarningMessageService.updateById(hyWarningMessage);
		return Result.OK("关闭成功!");
	}

}
